home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 364_01 / func_ca.c < prev    next >
C/C++ Source or Header  |  1992-05-24  |  14KB  |  486 lines

  1. /*
  2. HEADER:         ;
  3. TITLE:          C-ACROSS;
  4. VERSION         1.02
  5.  
  6. DESCRIPTION:   "Utility for multiple module programs. Produces
  7.       Six indexes of functions, prototypes, and globals that
  8.       enable user to 'see across' modules for use in checking
  9.       and comparison.  One of these is type of hierarchical
  10.       functions list, a listing by module of functions
  11.       and calls made FROM them; another is alphabetical list
  12.       of functions and calls made TO them. Globals listed
  13.       in schematic descriptors which record all modifiers
  14.       and qualifiers and enable checking of declarators
  15.       across modules. Creates, on request, header file
  16.       consisting of prototypes constructed from function
  17.       definitions. Can list user types #defined and some
  18.       preprocessor #defines. Full documentation in README.CA";
  19.  
  20. KEYWORDS:       Utility, Cross Reference, Deubgging;
  21. SYSTEM:         MS-DOS;
  22. FILENAME:       FUNC_CA.C;
  23.  
  24. WARNINGS:      "1. Assumes function definitions conform with
  25.         ANSI standards and have prototype form. See
  26.         also "Caveats and Restrictions" in README.CA.
  27.         2. Assumes syntactically correct source files.
  28.         3. Written and tested using Microsoft QuickC.
  29.         4. Copyright retained.  See Copyright
  30.         information in README.CA.";
  31.  
  32. SEE-ALSO:      EXIT_CA.C,  GLOB_CA.C, IFDEF_CA.C, INTF_CA.C,
  33.            LINKL_CA.C, PARSE_CA.C, TDEF_CA.C, TYPES_CA, UTIL_CA.C,
  34.            UTLG_CA.C, XRF_CA.C, README.CA,
  35.            CA.H, CA.PRJ, CA.RPT, CDECL_CA.H, KEYWORDS.H;
  36. AUTHORS:       Myron Turner;
  37. COMPILERS:     Microsoft C;
  38.  
  39. */
  40.  
  41. /* **************************  C-ACROSS  **************************
  42.                    V. 1.02
  43.                Copyright (C) Myron Turner
  44.  
  45.               333 Bartlet Ave.
  46.               Winnipeg, Manitoba
  47.               Canada R3L 0Z9
  48.               (204) 284-8387
  49.  
  50.  ******************************************************************** */
  51.  
  52.  
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <ctype.h>
  56. #include <stdlib.h>
  57. #include <graph.h>
  58.  
  59. #define TYPEDEF_DEFINE
  60. #define TDEF_EXTERNAL
  61. #define TDEF_NUM
  62. #define INCLUDEFILES_DEFINE
  63. #include "keywords.h"
  64. #define TESTRUN
  65. #include "ca.h"
  66. #include "ca_decl.h"
  67. #define FLAG_S static short
  68. #define UINT unsigned
  69.  
  70. #define OPEN_CURLY '{'
  71. extern FILE *scrn_out;
  72. extern int declare_file ;    /* create a C declarations file: /UHF switch */
  73. extern int __mods[_MAX_MODS];
  74. extern int reading_includeF;
  75. extern UINT int inclfLine;
  76. extern UINT int inclFini;
  77.  
  78. int skipifdefn(char *p, int skipdef_status);
  79. FLAG_S functionstart(char **hold_p, char **hold_tptr);
  80. int func_def(char *p, char *module, struct func **top,
  81.                    char *big_buf, FILE *rptfp);
  82.  
  83. static struct func *syntax(char *p, char * big_buf, char *module,
  84.                  struct func *top,  FILE *rptfp);
  85.  
  86. FILE **FP_ADDRESS;
  87. int function_no = 0;
  88. int In_Parentheses = 0;
  89. int In_MainLoop = 0;
  90. struct func *function = NULL, *FirstFunction = NULL;
  91. struct includeFILE inclF;
  92.  
  93. #define BIGBUFSIZE  350 + 261
  94. #define mod_type_check_off()   __mods[0] = 0
  95. #define INPARENTHESES -1
  96.  
  97.  
  98. /*   Group of variables used by several functions in this module:  the     */
  99. /*   gloabls eliminate need to pass excessive number of function arguments */
  100. /*   for variables which are needed in and changed by several functions    */
  101.  
  102. static char *routine;
  103. FLAG_S infunction = 0;
  104. FLAG_S open_parenthesis = 0;   /* = possible function */
  105. FLAG_S buffering = 0;   /* = more than one input line in possible function */
  106. FLAG_S inner_paren= 0;  /* track of parentheses within main parenthetical  */
  107. static int brackets = 0;
  108. static UINT endfunction = 0;
  109. static struct func *previous =  NULL;
  110. static UINT bufring_line_held=0;  /* holds 1st lineno of funct'n def when */
  111.                  /* definition takes up more than one line */
  112. FLAG_S inliteral = 0;
  113. static UINT int line_count = 0,  lineno;
  114. static char *token_ptr;
  115. static int t_pos = 0, m_pos = 0;
  116. static char buffer[261];
  117.  
  118. static void ini_Globals(void);
  119.  
  120. #define DEBUG_LINE 13
  121. char *debugptr;
  122.    /**********************FUNCTIONS******************************/
  123.  
  124.   /* Main Loop for first pass */
  125. struct func *main_loop(FILE *fp, FILE *rptfp, char *module)
  126. {
  127.  static struct func *top = NULL;
  128.  char  *p, big_buf[BIGBUFSIZE];
  129.  int  __type, skip_ifdef = 0;
  130.  FILE * hold_fp = NULL;
  131.  
  132.  ini_Globals();
  133.  FP_ADDRESS = &fp;
  134.  hold_fp = fp;
  135.  
  136.  memset( big_buf, '\0', BIGBUFSIZE );
  137.  ini_if_def_stack();
  138.  
  139.   _clearscreen(0);
  140.   relocate();
  141.   if (scrn_out != (FILE *)stdout) fprintf(scrn_out,
  142.                "\n**********\n\nFirst Pass: %s\n", module);
  143.   printf("First Pass: %s\n", module);
  144.  
  145.  
  146.  
  147. /* Main loop looks for the first indicator of a function definition,    */
  148. /* which is the type specifier.  It also handles comments and strings,  */
  149. /* keeps count of curly braces, in order to determine end of             */
  150. /* function body.                                                        */
  151.  
  152. startmainloop:
  153.  
  154.   while (  fgets (buffer, 160, fp) )
  155.   {
  156.  
  157.       p = buffer;
  158.       debugptr = p;
  159.       if (!reading_includeF) line_count++;
  160.      else
  161.      inclfLine++;
  162.      if (line_count == DEBUG_LINE)
  163.                    line_count = DEBUG_LINE;
  164.  
  165.      if (inclfLine == DEBUG_LINE)
  166.                    inclfLine = DEBUG_LINE;
  167.  
  168.       skip_ifdef = skipifdefn(p, skip_ifdef);
  169.       if (skip_ifdef) goto startmainloop;
  170.  
  171.  
  172.       /* count curly brackets to determine ending line of function body    */
  173.       /* infunction is set to 1 in func_def(), when a function definition  */
  174.       /* is found.                                                         */
  175.       if (infunction && *p != '\n') {
  176.        brackets += countbrackets(buffer);
  177.        if (!brackets)
  178.            endfunction = line_count;
  179.        if (!brackets) infunction = 0;
  180.        }
  181.  
  182.       /* handle comments and strings */
  183.       do {
  184.        if ( (inliteral = isliteral(p)) ) p++;
  185.        } while ( *p && inliteral );
  186.  
  187.       if (!inliteral)
  188.     if(buffering && open_parenthesis) In_Parentheses = 1;
  189.     while (iswhite(*p)) p++;
  190.  
  191.      /* move through modifiers;  */
  192.     if (!buffering) { m_pos = _modifier (p, 0); p += m_pos; }
  193.  
  194.     /*  if next element in line is a data type check for function def */
  195.     if ( (t_pos = data_type_(p, &__type)) || open_parenthesis)
  196.         top = syntax(p, big_buf, module, top, rptfp);
  197.  
  198.  
  199.    }
  200.  
  201.   /* check to see whether we are in an include file: if so, go back to */
  202.   /* start of the main_loop                                            */
  203.   if (close_include(hold_fp, &fp, rptfp, module))
  204.             goto startmainloop;
  205.  
  206.    print_userdefs(rptfp, module);
  207.  
  208.   if (previous) previous->endfunc_line = endfunction;
  209.   free_if_def_stack();
  210.   return (top);
  211. }
  212.  
  213.  
  214. static void make_function_token(char *token_ptr, char *function_name)
  215. {
  216.   char *p = token_ptr, *function_ptr = function_name;
  217.  
  218.   while (*p == '\x20' || *p == '\x9' || *p == '*') p++;
  219.  
  220.  while (  *p && *p != ' ' && *p != '('  )
  221.      *function_ptr++ = *p++;
  222.      *function_ptr = '\0';
  223.  
  224. }
  225.  
  226.  
  227. static void print_userdefs(FILE *rptfp, char *module)
  228. {
  229.  
  230.   int i, j = 0;
  231.   char *tm[] = {"",  "Modifier", "Type" };
  232.  
  233.   fprintf(scrn_out, "\nUSER DEFINED TYPES AND MODIFIERS IN: %s ", module);
  234.   for (i = 0; i < __typedef_count; i++) {
  235.     j = (__typ_mod[i] & 0xf);
  236.     fprintf(scrn_out,"\n%d: %s [ %s ]", i, __type_def[i], tm[j]);
  237.     }
  238.  
  239.    fprintf(scrn_out, "\n");
  240.    fprintf(scrn_out, "\n");
  241.  
  242. }
  243.  
  244. #define PTR_to_FN  !In_MainLoop
  245. struct func *fill_func_struct(char *token_ptr, int lineno, char *module)
  246. {
  247.   static struct func *top = NULL;
  248.  
  249.     function = (struct func *)
  250.          malloc ( sizeof (struct func) );
  251.     function->line_no = lineno;
  252.     /* list functions in order of appearance in files */
  253.     link_by_module(function);
  254.     function->first_call = NULL;
  255.     make_function_token(token_ptr, function->name);
  256.     strcpy (function->module, module);
  257.     if (PTR_to_FN) function->fnptr = 1;
  258.           else function->fnptr = 0;
  259.     top = store_linked_list(function, top);
  260.     function_no++;
  261.  
  262.      if (!FirstFunction) FirstFunction = function;
  263.   return(top);
  264. }
  265.  
  266.  
  267.  
  268. /* This function does syntactical analysis of the input line.            */
  269. /* If line includes possible function definition,  it is sent to         */
  270. /* func_def() to determine whether it holds a function definition.       */
  271.  
  272. static struc